Inheritance in Java
The mechanism of deriving a new class from an old one is called Inheritance. The old class is called the base class or superclass or parent class, and the new one is called the subclass or derived class or child class.
Advantage of Inheritance
Here is the list of key advantages of inheritance in Java programming language.
1. Reusability: The main advantage of inheritance is Reusability. Reusability means that we can add additional features of an existing class without modifying it.
2. Time-Saving: The above concept of reusability achieved by inheritance saves the programmer time and effort. Since the main code is written can be reused in various situations are needed. So saving time reduce cost and money.
3. Minimize Duplicate Code: If the duplicate code (variable and methods) exists in two related classes, we can refactor that hierarchy by moving that common code up to the common superclass.
4. Better Organized code: By moving the common code to the superclass results in the better organization of code. Which will be easy to understand by others.
5. Extendibility: We can extend the already made classes by adding more features easily.
6. Maintainability: It is easy to debug a program when divides in parts. Inheritance provides an opportunity to capture the problem.
Disadvantage of Inheritance
1. The inheritance relationship is a tightly coupled relationship; there will be tight bonding between parent and child.
2. As classes are tightly coupled, they cannot work independently of each other.
3. If we change the code of parent class it will get effects to all the child classes which are inheriting the parent code.
4. If a superclass method is deleted, Other classes code may not work as subclass may call the superclass method.
Types of Inheritance:
Java supports three types of inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance